package com.zhidisoft.crm.controller.auth;

import java.util.List;
import java.util.UUID;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhidisoft.crm.entity.SysUser;
import com.zhidisoft.crm.service.ISysUserService;
import com.zhidisoft.crm.vo.AjaxVo;
import com.zhidisoft.crm.vo.SysUserVo;

/**
 * auth/user/list get 默认列表(包括查询条件) <br/>
 * auth/user/${id} delete 删除<br/>
 * auth/user get 跳转到新增页面 <br/>
 * auth/user post data 新增<br/>
 * auth/user/${id} get 跳转到修改页面<br/>
 * auth/user/${id} post data 修改
 * 
 * @author REN
 *
 */

@Controller
@RequestMapping("/auth/user")
public class UserController {

	@Autowired
	ISysUserService iSysUserService;
	
	@GetMapping("/list")
	public ModelAndView list(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "pageSize", defaultValue = "2") Integer pageSize) {
			
		return new ModelAndView("auth/user/list.html","list",iSysUserService.pageList(pageNum, pageSize));
	}

	 @GetMapping
	 public String saveUI() {
		 return "auth/user/save.html";
	 }
	
	 @PostMapping
	 @ResponseBody
	 public AjaxVo save(@Valid SysUserVo sysUserVo,BindingResult result) {
		 if(result.hasErrors()){
				List<ObjectError> objectErrors = result.getAllErrors();
				for (ObjectError objectError : objectErrors) {
					return new AjaxVo().setErrorCode("1001").setErrorMsg(objectError.getDefaultMessage());
				}
			}
			sysUserVo.setId(UUID.randomUUID().toString().replace("-", ""));
			StandardPasswordEncoder spe = new StandardPasswordEncoder();
			sysUserVo.setPassword(spe.encode(sysUserVo.getPassword()));
			iSysUserService.save(sysUserVo);
			return new AjaxVo().setErrorCode("0").setErrorMsg("添加成功!");
	
	 }
	
	 @GetMapping("/update/{id}")
	 @ResponseBody
	 public ModelAndView updateUI(@PathVariable("id") String id) {
		 
		 return new ModelAndView("auth/user/update.html","user",iSysUserService.findId(id));
	 }
	
	 @PostMapping("/update/{id}")
	 @ResponseBody
	 public AjaxVo update(@Valid SysUser sysUser,@PathVariable("id") String id,BindingResult result) {
		 if(result.hasErrors()){
				List<ObjectError> objectErrors = result.getAllErrors();
				for (ObjectError objectError : objectErrors) {
					return new AjaxVo().setErrorCode("1001").setErrorMsg("修改失败");
				}
			}
		 		sysUser.setId(id);
		 		iSysUserService.update(sysUser);
			return new AjaxVo().setErrorCode("0").setErrorMsg("修改成功!");
		
	 }
	
	 /**
	  * 删除用户
	  * @param id
	  */
	 @DeleteMapping("/delete/{id}")
	 @ResponseBody
	 public void delete(@PathVariable("id") String id) {
		 iSysUserService.delete(id);
		 
	 }
	
}
